#!/usr/bin/env python

import time

from thrift import Thrift
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TFramedTransport, TMemoryBuffer
from thrift.protocol.TBinaryProtocol import TBinaryProtocol

from Thruqueue import Thruqueue, ttypes as ThruqueueTypes

THRUQUEUE_PORT = 9093;
THRUQUEUE_NAME = "test";

socket = TSocket('localhost', THRUQUEUE_PORT)
transport = TFramedTransport(socket)
protocol = TBinaryProtocol(transport)
myqueue = Thruqueue.Client(protocol)

transport.open()

myqueue.create(THRUQUEUE_NAME, 0);

myqueue.enqueue(THRUQUEUE_NAME, "first", 0)
myqueue.enqueue(THRUQUEUE_NAME, "second", 4)
myqueue.enqueue(THRUQUEUE_NAME, "third", 0)

try:
    while myqueue.length(THRUQUEUE_NAME):
        print myqueue.dequeue(THRUQUEUE_NAME)
        print myqueue.length(THRUQUEUE_NAME)
        
        time.sleep(1)
        if (int(time.time()) % 2 == 0):
            myqueue.enqueue(THRUQUEUE_NAME, "four", 0)
            
except Exception, e:
    myqueue.destroy(THRUQUEUE_NAME)
    print "what"
    print  e

